package com.cn.kabalifeuser.ui.integral.activity;

import com.cn.kabalifeuser.api.Api;
import com.cn.kabalifeuser.base.BasePresenter;
import com.cn.kabalifeuser.callback.CacheCallBack;
import com.cn.kabalifeuser.config.Constants;
import com.cn.kabalifeuser.ui.integral.bean.CartCheckBean;
import com.cn.kabalifeuser.ui.integral.bean.CartListBean;
import com.cn.kabalifeuser.ui.integral.bean.OrderPlaceBean;
import com.cn.kabalifeuser.ui.login.bean.MsgBean;
import com.cn.kabalifeuser.ui.release.bean.ReleaseListBean;
import com.cn.kabalifeuser.utils.LoggerUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

/**
 * Created by 晓林 on 2020/9/11.
 * function:
 */
public class CartListPresenter extends BasePresenter<CartListView> {

    public void onCartListData(String token, int page, int limit) {
        OkGo.<CartListBean>get(Api.API_ORDER_CART_LIST)
                .headers("token", token)
                .params("limit", limit)
                .params("page", page)
                .execute(new CacheCallBack<CartListBean>() {
                    @Override
                    public void onSuccess(Response<CartListBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("list：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onCartListSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    /**
     * 加入购物车
     *
     * @param token
     * @param type
     * @param good_num
     */
    public void onAddCartData(String token, String type, int good_num, String id) {
        OkGo.<MsgBean>post(Api.API_ORDER_CART_UP)
                .headers("token", token)
                .params("type", type)
//                .params("good_id", good_id)
//                .params("spec_id", spec_id)
                .params("num", good_num)
                .params("id", id)
                .execute(new CacheCallBack<MsgBean>() {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("加入购物车：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onAddCartSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    /**
     * 勾选的计算
     *
     * @param token
     * @param id
     */
    public void onCheckCartData(String token, String id) {
        OkGo.<CartCheckBean>get(Api.API_ORDER_CART_UP_CHECK)
                .headers("token", token)
                .params("id", id)
                .execute(new CacheCallBack<CartCheckBean>() {
                    @Override
                    public void onSuccess(Response<CartCheckBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("选中：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onCheckCartSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    /**
     * 结算
     * @param token
     * @param type
     * @param id
     */
    public void onOrderData(String token, String type, String id) {
        OkGo.<OrderPlaceBean>post(Api.API_ORDER_CART)
                .headers("token", token)
                .params("type", type)
                .params("id", id)
                .execute(new CacheCallBack<OrderPlaceBean>() {
                    @Override
                    public void onSuccess(Response<OrderPlaceBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("下单：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onOrderSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }
}
